<!DOCTYPE html>
<html>
<head>
    <title>农商互联开发平台</title>
    <link href="/static/css/framework-font.css" rel="stylesheet"/>
    <script src="/static/js/jquery-2.1.1.js"></script>
    <script src="/static/bootstrap/js/bootstrap.js"></script>
    <link href="/static/bootstrap/css/bootstrap.min.css" rel="stylesheet"/>

    <script src="/static/js/jquery.cookie.js"></script>
    <link href="/static/css/framework-theme.css" rel="stylesheet"/>
    <!--    <script src="/static/js/framework-ui.js"></script>-->
    <!--    <script src="/static/js/framework-clientdata.js"></script>-->

    <!--弹框js-->
    <script src="/static/js/yii.js"></script>

    <!--引用layui弹框插件-->
    <script src="/static/layui/layui.all.js"></script>
    <script src="/static/layui/layui.js"></script>
    <link href="/static/layui/css/layui.css" rel="stylesheet"/>
    <script src="/static/layui/lay/modules/layer.js"></script>


    <!--    配置select多选相关-->
    <!--    <script src="/static/layui/dist/xm-select.js"></script>-->

        {if condition="$swidthStyle eq 0"}
    <link href="/static/css/pattern/public.css" rel="stylesheet"/>
    {else/}
    <link href="/static/css/default/public.css" rel="stylesheet"/>
    {/if}


</head>
<body>
<style>
    .error {
        display: block;
        color: red
    }

    html, body {
        background-color: #FFF !important;
    }

    .modal-body {
        background-color: #fff;
        padding: 15px
    }

    .page-header {
        margin-top: 15px !important;
    }

    .geographic input {
        width: 100px !important;
        float: left;
        margin-right: 15px;
    }
    .config_img{margin-top:15px;width:100px;}
    .images_del{text-align: center}
</style>

<div class="modal-body">


    <div class="page-header">
        <h3>添加商品</h3>
    </div>

    <form class="form-horizontal layui-form" name="form1">


        <div class="form-group">

            <label class="col-sm-1 control-label">*所属景区:</label>
            <div class="col-sm-2">
                <select name="colony_id" lay-filter="colony_id">
                    {volist name="colony" id="vo"}
                    <option value="{$vo.id}">{$vo.name}</option>
                    {/volist}
                </select>
            </div>

            <label class="col-sm-1 control-label">*关联栏目:</label>
            <div class="col-sm-2">
                <select name="classify_id" lay-filter="classify_id">
                    {volist name="classify" id="vo"}
                    <option value="{$vo.id}" {if condition="$classify_id eq $vo['id']"} selected {/if} >{$vo.name}</option>
                    {/volist}
                </select>
            </div>

        </div>




        <div class="form-group">
            <label class="col-sm-1 control-label">*商品标题:</label>
            <div class="col-sm-2">
                <input type="text" class="form-control" name="title" placeholder="商品标题">
                <p class="error"></p>
            </div>
        </div>




        <div class="form-group">
            <label class="col-sm-1 control-label">缩略图:</label>
            <div class="col-sm-4">
                <div class="input-group ">
                    <input type="text" id="thumb" name="thumb" value="" class="form-control" autocomplete="off">
                    <span class="input-group-btn">
                        <button class="btn btn-default" id="upload" type="button" data-original-title="" title="">选择图片</button>
                    </span>
                </div>
                <img class="config_img thumb" src="/static/images/nopic-107.png" >
            </div>
        </div>

        <div class="form-group group_photo">
            <label class="col-sm-1 control-label">商品图片组:</label>
            <!--存储上传的图片信息-->
            <div class="col-sm-4">
                <div class="input-group ">
                    <input type="text" id="images" disabled name="images" value="" class="form-control" autocomplete="off">
                    <span class="input-group-btn">
                        <button class="btn btn-default" id="images_upload" type="button" data-original-title="" title="">选择图片</button>
                    </span>
                </div>
            </div>
        </div>

        <div class="form-group group_photo">
            <label class="col-sm-1 control-label"></label>
            <!--存储上传的图片信息-->
            <div class="col-sm-8">
                <div class="input-group show_multiple row">

                </div>
            </div>
        </div>


        <div class="form-group">
            <label class="col-sm-1 control-label">*所需积分:</label>
            <div class="col-sm-2">
                <input type="number" class="form-control" name="integral" placeholder="所需积分">
                <p class="error"></p>
            </div>

            <label class="col-sm-1 control-label">*兑换角色:</label>
            <div class="col-sm-2">
                <select name="type">
                    {volist name="type" id="vo"}
                        <option value="{$key}">{$vo}</option>
                    {/volist}
                </select>
                <p class="error"></p>
            </div>

            <label class="col-sm-1 control-label">*库存:</label>
            <div class="col-sm-2">
                <input type="number" class="form-control" name="inventory" placeholder="库存">
                <p class="error"></p>
            </div>

        </div>

        <div  class="form-group" >
            <label class="col-sm-1 control-label">排序:</label>
            <div class="col-sm-2">
                <input type="number" class="form-control" name="sort" placeholder="排序">
                <p class="error"></p>
            </div>

            <label class="col-sm-1 control-label">启用状态:</label>
            <div class="col-sm-2">
                <select name="status">
                    {volist name="audit" id="vo"}
                    <option value="{$key}">{$vo}</option>
                    {/volist}
                </select>
            </div>
        </div>


        <div  class="form-group" >
            <label class="col-sm-1 control-label">权益介绍:</label>
            <div class="col-sm-8">
                <textarea id="content" ></textarea>
            </div>
        </div>



</form>
</div>
<div class="add_button">

    <div class="form-group">
        <label class="col-sm-1 control-label"></label>
        <div class="col-sm-8">
            <a type="button" class="layui-btn layui-btn-primary" href="{:URL('community/exchange/index')}?classify_id={$classify_id}">取消</a>
            <button type="button" class="layui-btn" id="newsave">保存</button>
        </div>
    </div>
</div>


<script>

    var classify_id = "{$classify_id}";
    $(function () {
        $(this).find('.modal-dialog').css({
            'width': '1000px'
        });
    });
    var newModal = new yii.modal();
    $(".show_multiple").on("click",".images_del",function () {
        $(this).parent().remove();
    })
    //单图片选择
    $("#upload").click(function () {
        $(newModal.getid()).show();
        newModal.initialization({
            url: "{:URL('common/upload/index')}",
            type: "post",
            data:{
                type:'detail'
            }
        });
    });

    newModal.on("upload",function(data){
        var images = '';
        $(newModal.getid()).find("#data-area .newimages .active img").each(function () {
            images = $(this).attr('src');
            return false
        })
        $(newModal.getid()).hide();
        //$(".modal-backdrop").remove();
        //$('#nowMainProjectDataForm').modal('hide');
        $(newModal.getid()).find(".modal").hide();
        $("#thumb").val(images);
        $(".config_img").attr("src",images)
    });
    //单图片选择end

    //多图片上传
    $("#images_upload").click(function () {
        $(newModal.getid()).show();
        newModal.initialization({
            url: "{:URL('common/upload/index')}",
            type: "post",
            data:{
                type:'detail',
                multiple:'multiple'
            }
        });
    });

    newModal.on("multiple",function(data){
        var images = '';
        $(newModal.getid()).find("#data-area .newimages .active img").each(function () {
            images += '<div class="newimg"><img src="'+$(this).attr('src')+'" ><input type="hidden" name="images[]" value="'+$(this).attr('src')+'"><div class="images_del" >删除</div></div>';
        })
        $(newModal.getid()).hide();
        //遮罩层隐藏
        //$('#nowMainProjectDataForm').modal('hide');
        $(".modal-backdrop").remove();
        $(newModal.getid()).find(".modal").hide();
        $(".show_multiple").append(images);
    });
    //多图片上传end




    layui.extend({
        tinymce: '/static/tinymce/tinymce'
    });
    layui.use(['form', 'tinymce', 'tree', 'util', 'table','laydate'], function () {
        var form = layui.form
            , layer = layui.layer;
        var tinymce = layui.tinymce;
        var laydate = layui.laydate;

        tinymce.render({
            elem:'#content',
            height: 600,
            images_upload_handler: function (blobInfo, succFun, failFun) {
                var xhr, formData;
                var file = blobInfo.blob();//转化为易于理解的file对象
                xhr = new XMLHttpRequest();
                xhr.withCredentials = false;
                xhr.open('POST', "{:URL('/common/upload/textUpload')}");
                xhr.onload = function() {
                    var json;
                    json = JSON.parse(xhr.responseText);
                    succFun(json.path);
                };
                formData = new FormData();
                formData.append('file', file, file.name );//此处与源文档不一样
                xhr.send(formData);
            }
        },(opt, edit) => {

        });

        $("#newsave").click(function () {
            var content = tinymce.get('#content').getContent();
            var data = $("form[name='form1']").serializeArray();
            data.push({name: "content", value: content});
            $.ajax({
                type: 'post',
                url: "{:URL('community/Exchange/doExchange')}",
                data: data,
                dataType: 'JSON',
                success: function (res) {
                    if (res.code != 0) {
                        layer.alert(res.msg);
                    } else {
                        window.location.href = "{:URL('community/Exchange/index')}?classify_id="+classify_id;
                    }
                },
                error: function (res) {

                }
            });
        });



        //获取小区
        form.on('select(colony_id)', function (data) {
            //触发联动
            var id = data.value;
            $.ajax({
                type: 'post',
                url: "{:URL('common/together/findClassify')}",
                data: {id: id},
                dataType: 'JSON',
                success: function (res) {
                    //获取相应栏目
                    var classify = res.classify;
                    //生成楼栋信息
                    var html1 = '<option value="">请选择栏目</option>';
                    $.each(classify, function (e, v) {
                        html1 += '<option value="' + v.id + '">' + v.name + '</option>';
                    });
                    $('select[name="classify_id"]').html(html1);

                    form.render('select');
                },
                error: function (res) {
                }
            });
        });


    });
</script>

</body>
</html>